<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Coordinator_m extends CI_Model {
        
        function get_my_account($id) {
            $sql = "SELECT * FROM coordinator WHERE coordinator_id = ?";
            $q = $this->db->query($sql, $id);
            if($q->num_rows() > 0) {
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_sender_or_recipient($id) {
            $sql = 'SELECT * FROM coordinator WHERE coordinator_id = ?';
            $q = $this->db->query($sql, $id);
            if ($q->num_rows() > 0) {
                return $q->row();
            } else {
                return FALSE;
            }
        }
        
        function get_recipients($id) {
            $sql = 'SELECT * FROM coordinator WHERE coordinator_id != ? AND coord_status = "0"';
            $q = $this->db->query($sql, $id);
            if ($q->num_rows() > 0) {
                return $q->result_array();
            } else {
                return FALSE;
            }
        }
        
        function get_employees($id) {
            $sql = "SELECT coordinator_id, CONCAT(coord_lname,', ',coord_fname,' ',coord_mname) AS name, 
                coord_bday, coord_gender, coord_email, coord_address, coord_num, emp_date, coord_status, coord_lname,
                coord_fname, coord_mname FROM coordinator WHERE coordinator_id != ? AND coord_status = '0' ORDER BY coord_lname";
            $q = $this->db->query($sql, $id);
            if($q->num_rows() > 0) {
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_employee($id) {
            $q = $this->db->select('*, MONTH(coord_bday) AS month, DAY(coord_bday) AS day, YEAR(coord_bday) AS year,')
                    ->where('coordinator_id', $id)->get('coordinator');
            if($q->num_rows() > 0) {
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function check_if_emp_exist($lname, $fname, $mname){
            $q = $this->db->where('coord_lname', $lname)->where('coord_fname', $fname)->where('coord_mname', $mname)
                    ->get('coordinator');
            return $q->num_rows();
        }
        function insert_emp($data) {
            $this->db->insert('coordinator', $data);
            return $this->db->insert_id();
        }
        
        function delete_employee($emp_id) {
            for($i=0;$i<sizeof($emp_id);$i++){
                $this->db->set('coord_status', '1')->where('coordinator_id', $emp_id[$i])->update('coordinator');
            }
            return $this->db->affected_rows();
        }
        
        function update_emp($id, $data) {
            $emp = array(
                'coord_lname' => $data['coord_lname'],
                'coord_fname' => $data['coord_fname'],
                'coord_mname' => $data['coord_mname'],
                'coord_bday' => $data['coord_bday'],
                'coord_gender' => $data['coord_gender'],
                'coord_address' => $data['coord_address'],
                'coord_num' => $data['coord_num'],
                'coord_email' => $data['coord_email'],
                'emp_date' => $data['emp_date'],
            );
            $this->db->where('coordinator_id', $id)->update('coordinator', $emp);
            return $this->db->affected_rows();
        }
        
        function update_my_account($id, $data) {
            $this->db->where('coordinator_id', $id)->update('coordinator', $data);
            return $this->db->affected_rows();
        }
        
        function change_password($id, $pass) {
            $this->db->set('coord_pw', sha1($pass))->where('coordinator_id', $id)
                    ->update('coordinator');
            return $this->db->affected_rows();
        }
        
        function check_old_password($id, $pass) {
            $q = $this->db->where('coordinator_id', $id)->where('coord_pw', sha1($pass))
                    ->get('coordinator');
            if($q->num_rows() > 0){
                return 'valid';
            }else{
                return 'invalid';
            }
        }
        
        function validate_credentials($data) {
            $q = $this->db->where('coord_email', $data['email'])->where('coord_pw', sha1($data['pass']))
                    ->get('coordinator');
            if ($q->num_rows() > 0) {
                return $q->row();
            } else {
                return FALSE;
            }
        }
        
        function eve_privilege($coord_id){
            $q = $this->db->select('*')->from('privileges')->join('coord_privilege', 
                    'privileges.privilege_id=coord_privilege.privilege_id')
                    ->where('coordinator_id', $coord_id)
                    ->where('coord_privilege.privilege_id', 2)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function con_privilege($coord_id){
            $q = $this->db->select('*')->from('privileges')->join('coord_privilege', 
                    'privileges.privilege_id=coord_privilege.privilege_id')
                    ->where('coordinator_id', $coord_id)
                    ->where('coord_privilege.privilege_id', 3)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function res_privilege($coord_id){
            $q = $this->db->select('*')->from('privileges')->join('coord_privilege', 
                    'privileges.privilege_id=coord_privilege.privilege_id')
                    ->where('coordinator_id', $coord_id)
                    ->where('coord_privilege.privilege_id', 4)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function mes_privilege($coord_id){
            $q = $this->db->select('*')->from('privileges')->join('coord_privilege', 
                    'privileges.privilege_id=coord_privilege.privilege_id')
                    ->where('coordinator_id', $coord_id)
                    ->where('coord_privilege.privilege_id', 5)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function gue_privilege($coord_id){
            $q = $this->db->select('*')->from('privileges')->join('coord_privilege', 
                    'privileges.privilege_id=coord_privilege.privilege_id')
                    ->where('coordinator_id', $coord_id)
                    ->where('coord_privilege.privilege_id', 6)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function validate_delete($pass, $acc_id) {
            $q = $this->db->where('coordinator_id', $acc_id)->where('coord_pw', sha1($pass))->get('coordinator');
            if ($q->num_rows() > 0) {
                return 'valid';
            } else {
                return 'invalid';
            }
        }
    }
